/**
 * DrawingEngine - Canvas绘图引擎
 * 提供基础的绘图API，封装Canvas 2D上下文操作
 */

class DrawingEngine {
  constructor(ctx) {
    this.ctx = ctx
    this.stateStack = []
  }

  /**
   * 绘制圆形
   */
  drawCircle(x, y, radius, color, options = {}) {
    const { fill = true, stroke = false, strokeWidth = 1, strokeColor = '#000' } = options
    
    this.ctx.beginPath()
    this.ctx.arc(x, y, radius, 0, Math.PI * 2)
    
    if (fill) {
      this.ctx.setFillStyle(color)
      this.ctx.fill()
    }
    
    if (stroke) {
      this.ctx.setStrokeStyle(strokeColor)
      this.ctx.setLineWidth(strokeWidth)
      this.ctx.stroke()
    }
  }

  /**
   * 绘制椭圆
   */
  drawEllipse(x, y, radiusX, radiusY, color, options = {}) {
    const { fill = true, stroke = false, strokeWidth = 1, strokeColor = '#000', rotation = 0 } = options
    
    this.save()
    this.translate(x, y)
    this.rotate(rotation)
    
    this.ctx.beginPath()
    this.ctx.scale(radiusX / radiusY, 1)
    this.ctx.arc(0, 0, radiusY, 0, Math.PI * 2)
    this.ctx.scale(radiusY / radiusX, 1)
    
    if (fill) {
      this.ctx.setFillStyle(color)
      this.ctx.fill()
    }
    
    if (stroke) {
      this.ctx.setStrokeStyle(strokeColor)
      this.ctx.setLineWidth(strokeWidth)
      this.ctx.stroke()
    }
    
    this.restore()
  }

  /**
   * 绘制圆角矩形
   */
  drawRoundRect(x, y, width, height, radius, color, options = {}) {
    const { fill = true, stroke = false, strokeWidth = 1, strokeColor = '#000' } = options
    
    this.ctx.beginPath()
    this.ctx.moveTo(x + radius, y)
    this.ctx.lineTo(x + width - radius, y)
    this.ctx.arcTo(x + width, y, x + width, y + radius, radius)
    this.ctx.lineTo(x + width, y + height - radius)
    this.ctx.arcTo(x + width, y + height, x + width - radius, y + height, radius)
    this.ctx.lineTo(x + radius, y + height)
    this.ctx.arcTo(x, y + height, x, y + height - radius, radius)
    this.ctx.lineTo(x, y + radius)
    this.ctx.arcTo(x, y, x + radius, y, radius)
    this.ctx.closePath()
    
    if (fill) {
      this.ctx.setFillStyle(color)
      this.ctx.fill()
    }
    
    if (stroke) {
      this.ctx.setStrokeStyle(strokeColor)
      this.ctx.setLineWidth(strokeWidth)
      this.ctx.stroke()
    }
  }

  /**
   * 创建线性渐变
   */
  createLinearGradient(x1, y1, x2, y2, colorStops) {
    const gradient = this.ctx.createLinearGradient(x1, y1, x2, y2)
    colorStops.forEach(stop => {
      gradient.addColorStop(stop.offset, stop.color)
    })
    return gradient
  }

  /**
   * 创建径向渐变
   */
  createRadialGradient(x1, y1, r1, x2, y2, r2, colorStops) {
    const gradient = this.ctx.createRadialGradient(x1, y1, r1, x2, y2, r2)
    colorStops.forEach(stop => {
      gradient.addColorStop(stop.offset, stop.color)
    })
    return gradient
  }

  /**
   * 设置阴影
   */
  setShadow(offsetX, offsetY, blur, color) {
    this.ctx.setShadow(offsetX, offsetY, blur, color)
  }

  /**
   * 清除阴影
   */
  clearShadow() {
    this.ctx.setShadow(0, 0, 0, 'transparent')
  }

  /**
   * 保存状态
   */
  save() {
    this.ctx.save()
    this.stateStack.push({})
  }

  /**
   * 恢复状态
   */
  restore() {
    this.ctx.restore()
    this.stateStack.pop()
  }

  /**
   * 平移
   */
  translate(x, y) {
    this.ctx.translate(x, y)
  }

  /**
   * 旋转
   */
  rotate(angle) {
    this.ctx.rotate(angle)
  }

  /**
   * 缩放
   */
  scale(x, y) {
    this.ctx.scale(x, y)
  }

  /**
   * 清空指定区域
   */
  clear(x, y, width, height) {
    this.ctx.clearRect(x, y, width, height)
  }

  /**
   * 清空整个画布
   */
  clearAll(width, height) {
    this.ctx.clearRect(0, 0, width, height)
  }

  /**
   * 设置透明度
   */
  setAlpha(alpha) {
    this.ctx.setGlobalAlpha(alpha)
  }

  /**
   * 设置混合模式
   */
  setBlendMode(mode) {
    this.ctx.setGlobalCompositeOperation(mode)
  }

  /**
   * 绘制路径
   */
  drawPath(points, color, options = {}) {
    const { fill = false, stroke = true, strokeWidth = 1, closed = false } = options
    
    if (points.length < 2) return
    
    this.ctx.beginPath()
    this.ctx.moveTo(points[0].x, points[0].y)
    
    for (let i = 1; i < points.length; i++) {
      this.ctx.lineTo(points[i].x, points[i].y)
    }
    
    if (closed) {
      this.ctx.closePath()
    }
    
    if (stroke) {
      this.ctx.setStrokeStyle(color)
      this.ctx.setLineWidth(strokeWidth)
      this.ctx.stroke()
    }
    
    if (fill) {
      this.ctx.setFillStyle(color)
      this.ctx.fill()
    }
  }

  /**
   * 绘制贝塞尔曲线
   */
  drawBezierCurve(startX, startY, cp1X, cp1Y, cp2X, cp2Y, endX, endY, color, strokeWidth = 1) {
    this.ctx.beginPath()
    this.ctx.moveTo(startX, startY)
    this.ctx.bezierCurveTo(cp1X, cp1Y, cp2X, cp2Y, endX, endY)
    this.ctx.setStrokeStyle(color)
    this.ctx.setLineWidth(strokeWidth)
    this.ctx.stroke()
  }

  /**
   * 绘制文本
   */
  drawText(text, x, y, color, options = {}) {
    const { fontSize = 12, fontFamily = 'sans-serif', align = 'left', baseline = 'top' } = options
    
    this.ctx.setFontSize(fontSize)
    this.ctx.setTextAlign(align)
    this.ctx.setTextBaseline(baseline)
    this.ctx.setFillStyle(color)
    this.ctx.fillText(text, x, y)
  }
}

export default DrawingEngine
